//
// Created by hunan on 25-7-20.
//

#ifndef PROTOCOL_CONNECTION_POOL_H
#define PROTOCOL_CONNECTION_POOL_H

#include "protocol_operation_factory.h"

#include <memory>
#include <string>
#include <vector>
#include <mutex>

class Loop;
class ProtocolConnection;
class ProtocolOperation;
class ProtocolConnectionPool
{
private:
    struct Connection
    {
        std::shared_ptr<ProtocolOperation> m_operate;
        std::shared_ptr<ProtocolConnection> m_connection;
        int m_index{0};
    };
public:
    explicit ProtocolConnectionPool(Loop * loop);
    bool                               InitProtocolConnectionPool(int size, ProtocolOperationFactory * factory);
    std::shared_ptr<ProtocolConnection> GetConnection();
    void ReleaseConnection(const std::shared_ptr<ProtocolConnection> & connection);
    void CloseConnection();
private:
    Loop * m_loop;
    std::mutex m_mutex;
    std::vector<Connection> m_connections;
    std::vector<int> m_indexes;
};

#endif //PROTOCOL_CONNECTION_POOL_H
